/**   
 * 项目名：化工企业安全生产信息化管理平台
 * 日期：    2020-03-01 21:14:38  
 * Copyright (c) 2015- -版权所有     
 */

package com.joysuch.wwyt.common.controller;

import java.util.List;

import javax.validation.Valid;

import cn.zlg.common.springmvc.validate.constraints.NotNull;
import com.joysuch.wwyt.common.bean.CommonTypeEditBean;
import com.joysuch.wwyt.common.bean.CommonTypeListBean;
import com.joysuch.wwyt.common.bean.CommonTypeListBean.SearchCommonType;
import com.joysuch.wwyt.common.enums.CommonTypes;
import com.joysuch.wwyt.common.service.CommonTypeService;
import com.joysuch.wwyt.core.bean.SimpleSelectListBean;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.service.SimpleSelectListService;
import jodd.util.StringUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.util.Assert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/common/type")
public class CommonTypeController {

    private static final Logger logger = LoggerFactory.getLogger(CommonTypeController.class);

    @Autowired
    private CommonTypeService commonTypeService;
    @Autowired
    private SimpleSelectListService simpleSelectListService;

    //@RequiresPermissions("common:type:add")
    @RequestMapping("add")
    public Object add(@Valid @RequestBody CommonTypeEditBean entity) {
        commonTypeService.add(entity);
        return ResultBean.success("");
    }

    //@RequiresPermissions("common:type:update")
    @RequestMapping("update")
    public Object update(@Valid @RequestBody CommonTypeEditBean entity) {
        if (entity.getId() == null) {
            return ResultBean.fail(102, "未指定主键");
        }
        commonTypeService.update(entity);
        return ResultBean.success("");
    }

    //@RequiresPermissions("common:type")
    @RequestMapping("page")
    public Object page(Pageable page, SearchCommonType condition) {
        Page<CommonTypeListBean> data = commonTypeService.findByPage(page, condition);
        return ResultBean.pageData(data.getContent(), data.getTotalElements());
    }

    //@RequiresPermissions("common:type")
    @RequestMapping("select-page")
    public Object selectPage(Pageable page, SearchCommonType condition) {
        Page<CommonTypeListBean> data = commonTypeService.findByPage(page, condition);
        return ResultBean.pageData(data.getContent(), data.getTotalElements());
    }

    @RequestMapping("simple-list")
    public Object simpleList( String type) {
        CommonTypes ct = CommonTypes.findByName(type);
        Assert.notNull(ct, "未知的类型:" + type);
        List<SimpleSelectListBean> data = commonTypeService.simpleList(ct);
        return ResultBean.success(data);
    }

    //@RequiresPermissions("common:type:delete")
    @RequestMapping("delete")
    public Object delete(Long id) throws Exception {
        commonTypeService.delete(id);
        return ResultBean.success("");
    }

    //@RequiresPermissions("common:type")
    @RequestMapping("get")
    public Object getById(@Valid @NotNull(field = "id") Long id) {
        return ResultBean.success(commonTypeService.getById(id));
    }
}
